<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('参数设置')"/>
    <style>
        .layui-table-cell .layui-form-checkbox[lay-skin=primary]{top:4px;}
    </style>
</head>
<body>
<div class="layui-layout layui-layout-iframe">
    <div class="layui-fluid">
        <div class="layui-card">
            <!--search form-->
            <form class="layui-form layui-card-header layuiadmin-card-header-auto search-form" id="searchForm">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">参数名称</label>
                        <div class="layui-input-inline">
                            <input class="layui-input" type="text" name="dictName" placeholder="" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">参数键名</label>
                        <div class="layui-input-inline">
                            <input class="layui-input" type="text" name="configKey" placeholder="" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">系统内置</label>
                        <div class="layui-input-inline input-sm">
                            <select name="configType" th:with="type=${@dict.getType('sys_yes_no')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-inline mt5">
                        <label class="layui-form-label">创建时间</label>
                        <div class="layui-input-inline input-sm">
                            <input class="layui-input" type="text" name="params[beginTime]" id="startTime" placeholder="开始时间" />
                        </div>
                        <span class="layui-form-mid">-</span>
                        <div class="layui-input-inline input-sm">
                            <input class="layui-input" type="text" name="params[endTime]" id="endTime" placeholder="结束时间"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button  type="button" class="layui-btn layuiadmin-btn-list" data-type="reload" lay-submit lay-filter="form-search" id="form-search">搜索</button>
                    </div>
                </div>
            </form>
            <!--table actcion-->
            <div class="layui-card-body">
                <script type="text/html" id="table_config_toolbar">
                    <div class="layui-btn-group" shiro:hasPermission="system:config:add">
                        <button class="layui-btn layui-btn-sm" lay-event="toolbar-config-add" shiro:hasPermission="system:config:add"><i class="layui-icon">&#xe654;</i>新增</button>
                        <button class="layui-btn layui-btn-sm" lay-event="toolbar-config-remove" shiro:hasPermission="system:config:remove"><i class="layui-icon">&#xe640;</i>删除</button>
                    </div>
                </script>
                <table id="table_config" lay-filter="table_config"></table>
                <script type="text/html" id="col_operation">
                    <a shiro:hasPermission="system:config:edit" href="javascript:;" lay-event="edit">编辑</a><span class="splitor">|</span>
                    <a shiro:hasPermission="system:config:remove" href="javascript:;" lay-event="del">删除</a>
                </script>
            </div>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:config:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:config:remove')}]];
    var prefix = ctx + "system/config";
</script>
<script>
    layui.use(['common'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            form = layui.form,
            common = layui.common,
            laydate = layui.laydate,
            table = layui.table;
        //页面事件列表
        var active = {
            reload: function () {
                search(false);
            },
            remove: function (ids) {
                layui.common.ajaxRemove(prefix + "/remove", ids, function (res) {
                    active['reload'].call(this);
                });
            }
        };
        //开始日期
        var insStart = laydate.render({
            elem: '#startTime'
            , min: 0
            , done: function (value, date) {
                //更新结束日期的最小日期
                insEnd.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                insEnd.config.elem[0].focus();
            }
        });
        //结束日期
        var insEnd = laydate.render({
            elem: '#endTime'
            , min: 0
            , done: function (value, date) {
                //更新开始日期的最大日期
                insStart.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });
        //查询表单
        form.on('submit(form-search)', function (data) {
            search(true);
        });
        //表格初始化
        table.render({
            elem: '#table_config'
            , method: 'post'
            , url: prefix + "/list" //数据接口
            , toolbar: '#table_config_toolbar'
            , defaultToolbar:['filter', 'exports']
            , autoSort: false 	//关闭前端排序 使用后端排序
            , limit: getPageSize()
            , limits: getPageList()
            , text: getLoadErrorMsg()
            , cols: new Array([
                {type: 'checkbox', width: 40}
                , {field: 'configName', title: '参数名称'}
                , {field: 'configKey', title: '参数键名'}
                , {field: 'configValue', title: '参数键值'}
                , {field: 'configType', title: '系统内置', width: 100, align: 'center',
                    templet: function (item) {
                        if (item.configType === "Y") {
                            return "<span class=\"layui-btn layui-btn-xs layui-btn-radius\">是</span>";
                        } else {
                            return "<span class=\"layui-btn layui-btn-xs layui-btn-danger layui-btn-radius\">否</span>";
                        }
                    }
                }
                , {field: 'remark', title: '备注'}
                , {field: 'createTime', title: '创建时间', sort: true, width: 160}
                , {title: '操作', align: 'center', toolbar: '#col_operation', width: 100}
            ])
            ,parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.total, //解析数据长度
                    "data": res.rows //解析数据列表
                };
            }
        });
        //表格工具栏按钮事件
        table.on('toolbar(table_config)', function (obj) {
            switch (obj.event) {
                case 'toolbar-config-remove':
                    var checks = table.checkStatus('table_config');
                    var ids = common.joinArray(checks.data, "configId");
                    active['remove'].call(this, ids);
                    break;
                case 'toolbar-config-add':
                    showSaveDialog('修改系统参数', prefix + '/add', 'btn-add');
                    break;
                case 'toolbar_del':
                    break;
            }
        });
        //表格事件
        table.on('tool(table_config)', function (obj) {
            switch (obj.event) {
                case 'del':
                    active['remove'].call(this, obj.data.configId);
                    break;
                case 'edit':
                    showSaveDialog('修改系统参数', prefix + '/edit/' + obj.data.configId, 'btn-edit');
                    break;
            }
        });
        //监听排序事件
        table.on('sort(table_config)', function (obj) {
            var field = serializeJson($('#searchForm'));
            field.orderByColumn = obj.field ;
            field.isAsc = obj.type;
            table.reload('table_config', {where: field,page:{curr: 1}});//执行重载
        });
    });

    //展示保存表单弹出层
    function showSaveDialog(title, url, formSubmitBtnId) {
        layer.open({
            type: 2,
            title: title,
            shade: false,
            fixed: false,
            area: ['800px', '450px'],
            content: url,
            btn: ["保存", "取消"],
            yes: function (index, layero) {
                var submitID = formSubmitBtnId;//表单提交按钮id
                var submit = layero.find('iframe').contents().find('#' + submitID);//button obj
                layero.find('iframe')[0].contentWindow.successCallback = function () {
                    parent.layer.closeAll();
                    parent.layer.alert('保存成功');
                    parent.search(); //刷新表格数据
                };
                submit.trigger('click');
            }
            , cancel: function (index, layero) {
                layui.layer.close(index);
                return false;
            }
        });
    }

    //表格查询
    function search(isResetCurr) {
        var field = serializeJson($('#searchForm'));
        var sortParam = getSort('table_config');
        if (sortParam) {
            field.orderByColumn = sortParam.sortField;
            field.isAsc = sortParam.sortMethod;
        }
        //执行重载
        var options = {where: field};
        if (isResetCurr == true) {
            options.page = {curr: 1};
        }
        layui.table.reload('table_config', options);
    }
</script>
</body>
</html>